package com.bootdo.common.controller;

import com.bootdo.common.config.Constant;
import com.bootdo.common.utils.R;
import com.bootdo.program.wallpaper.domain.BzConfigDO;
import com.bootdo.program.wallpaper.service.BzConfigService;
import com.bootdo.system.domain.UserToken;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.domain.UserDO;

import java.util.List;

@Controller
public class BaseController {

	@Autowired
	private BzConfigService bzConfigService;


	public UserDO getUser() {
		return ShiroUtils.getUser();
	}

	public Long getUserId() {
		return getUser().getUserId();
	}

	public String getUsername() {
		return getUser().getUsername();
	}

	public String getAppid() {
		return null == getUser().getAppid() || "".equals(getUser().getAppid()) ? null : getUser().getAppid();
	}

	/**
	 * 是否超级管理，根据用户信息返回小程序appid
	 */
	public String  defaultAppid(){
		 return isAdmin() ? "wxe763c88aa16d0565" : getAppid();
	}

	public boolean  isAdmin(){
		boolean isAdmin = "admin".equals(getUser().getUsername()) || 1 == getUserId();
		return isAdmin;
	}

	public String getHttpUrl(){
		List<BzConfigDO> apiUrl = bzConfigService.getType("api_oss_type");
		return apiUrl.size() > 0 ? apiUrl.get(0).getValue() : "";
	}

	/**
	 * 获取当前用户appid  超级管路员返回null
	 * @return
	 */
	public String queryAppid() {
		if(isAdmin()){
			return null;
		}
		return null == getUser().getAppid() || "".equals(getUser().getAppid()) ? "fond not appid" : getAppid();
	}

	/**
	 * 验证操作是否合理
	 * @return
	 */
	public String getMessage(){
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return "演示系统不允许修改,完整体验请部署程序";
		}
		if(isAdmin()){
			return "超级管理员无权操作";
		}
		if(null == getUser().getAppid() || "".equals(getUser().getAppid())){
			return "保存失败，请先配置小程序appid";
		}
		return null;
	}
}